#include "../Include/common_pub.h"
#include <stdio.h>
#include <stdarg.h>
#include <time.h>

void log_message(LogLevel level, const char* format, ...) {
    FILE* log_file = fopen("log.log", "a");
    if (!log_file) return;
    
    // 时间戳
    time_t now = time(NULL);
    struct tm* tm_info = localtime(&now);
    char timestamp[20];
    strftime(timestamp, 20, "%Y-%m-%d %H:%M:%S", tm_info);
    
    // 日志级别
    const char* level_str;
    switch (level) {
        case LOG_INFO: level_str = "INFO"; break;
        case LOG_WARNING: level_str = "WARN"; break;
        case LOG_ERROR: level_str = "ERROR"; break;
        default: level_str = "INFO";
    }
    
    // 写入日志头
    fprintf(log_file, "[%s] [%s] ", timestamp, level_str);
    
    // 写入日志内容
    va_list args;
    va_start(args, format);
    vfprintf(log_file, format, args);
    va_end(args);
    
    fprintf(log_file, "\n");
    fclose(log_file);
    
    // 同时在控制台输出
    printf("[%s] ", level_str);
    va_start(args, format);
    vprintf(format, args);
    va_end(args);
    printf("\n");
}
